package net.k3rnel.abugadro.gcodes;

import net.k3rnel.abugadro.AbugadroContentProvider;
import net.k3rnel.abugadro.ProjectsDB;
import net.k3rnel.abugadro.R;
import net.k3rnel.abugadro.gcodes.beans.GoogleCode.GCode;
import net.k3rnel.abugadro.util.SyncService;
import net.k3rnel.abugadro.util.UIUtils;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

public class GoogleCodeHome extends Activity {

	private int projectId;
	private String projName;
	private int gCodeId;
	private final SyncService mSyncService = new SyncService();
	private Uri gCodeUri;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_gcode_home);

		final Uri projectUri = getIntent().getData();
		projectId = Integer.parseInt(projectUri.getPathSegments().get(1));
		projName = ProjectsDB.getInstance().getProjectName(getContentResolver(), projectId);
		((TextView) findViewById(R.id.title_text)).setText(projName);
		gCodeId = ProjectsDB.getInstance().getGoogleIdbyProjectId(getContentResolver(),projectId);
		gCodeUri = GCode.buildFullUri(gCodeId);
		if(!ProjectsDB.getInstance().isSyncedGcode(getContentResolver(),gCodeId))
			mSyncService.startThread(handler, getContentResolver(), gCodeUri, 0);
	}

	@Override
	public boolean moveTaskToBack(boolean nonRoot) {

		return super.moveTaskToBack(nonRoot);
	}

	/** Handle "refresh" title-bar action. */
	public void onRefreshClick(View v) {
		if(!mSyncService.isSync())
			mSyncService.startThread(handler, getContentResolver(), gCodeUri, 0);
	}
	/** Handle "progress" title-bar action. */
	public void onProgressClick(View v) {
		mSyncService.stopThread();
	}
	/** Handle "search" title-bar action. */
	public void onSearchClick(View v) {
		//		UIUtils.goSearch(this);
		AlertDialog ad = new AlertDialog.Builder(this)
		.setPositiveButton("Okay", null)
		.setTitle("Sorry Dude!")
		.setMessage("Search function isn't ready!")
		.create();
		ad.show();
	}

	/** Handle "home" title-bar action. */
	public void onHomeClick(View v) {
		UIUtils.goHome(this);
	}

	/** Handle "open" title-bar action. */
	public void onOpenClick(View v) {
		final Uri issuesURI = Uri.parse("content://"+ AbugadroContentProvider.AUTHORITY + "/gcode/"+gCodeId+"/issues/");
		Intent i = new Intent(getApplicationContext(),IssuesList.class);
		i.setAction("net.k3rnel.abugadro.OPEN");
		i.setDataAndType(issuesURI, "vnd.android.cursor.dir/*");
		startActivity(i);
	}
	/** Handle "closed" title-bar action. */
	public void onClosedClick(View v) {
		final Uri issuesURI = Uri.parse("content://"+ AbugadroContentProvider.AUTHORITY + "/gcode/"+gCodeId+"/issues/");
		Intent i = new Intent(getApplicationContext(),IssuesList.class);
		i.setAction("net.k3rnel.abugadro.CLOSED");
		i.setDataAndType(issuesURI, "vnd.android.cursor.dir/*");
		startActivity(i);
	}
	/** Handle "commits" title-bar action. */
	public void onCommitsClick(View v) {
		final Uri issuesURI = Uri.parse("content://"+ AbugadroContentProvider.AUTHORITY + "/gcode/"+gCodeId+"/code/");
		Intent i = new Intent(getApplicationContext(),CodeChangeList.class);
		i.setAction(Intent.ACTION_VIEW);
		i.setDataAndType(issuesURI, "vnd.android.cursor.dir/*");
		startActivity(i);
	}
	/** Handle "wiki" title-bar action. */
	public void onWikiClick(View v) {
		AlertDialog ad = new AlertDialog.Builder(this)
		.setPositiveButton("Okay", null)
		.setTitle("Hold it!")
		.setMessage("Wiki function isn't ready yet!")
		.create();
		ad.show();
	}
	/** Handle "alert" title-bar action. */
	public void onAlertClick(View v) {
		AlertDialog ad = new AlertDialog.Builder(this)
		.setPositiveButton("Okay", null)
		.setTitle("Hold it!")
		.setMessage("No Internets. Will work with offline data")
		.create();
		ad.show();
		updateRefreshStatus();
	}
	private void updateRefreshStatus() {
		findViewById(R.id.gcode_home_btn_alert).setVisibility(View.GONE);
		findViewById(R.id.gcode_home_btn_refresh).setVisibility(
				mSyncService.isSync() ? View.GONE : View.VISIBLE);
		findViewById(R.id.gcode_home_btn_refresh_progress).setVisibility(
				mSyncService.isSync() ? View.VISIBLE : View.GONE);
	}
	private void displayAlert() {
		findViewById(R.id.gcode_home_btn_refresh).setVisibility(View.GONE);
		findViewById(R.id.gcode_home_btn_refresh_progress).setVisibility(View.GONE);
		findViewById(R.id.gcode_home_btn_alert).setVisibility(View.VISIBLE);
	}

	final Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			if(msg.getData().getInt("status")==1)
				updateRefreshStatus();
			else
				displayAlert();
		}
	};
}